package lwh.adapter;

import java.util.List;

import lwh.activity.MyGlobal;
import lwh.activity.R;
import lwh.domain.ContractChild;
import lwh.domain.ContractGroup;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ChildSubsectionProjectSimpleExpandableListAdapter extends BaseExpandableListAdapter {
	
	private List<ContractGroup> list;
	
    private LayoutInflater inflater;
    
    private MyGlobal global;
    
    private Activity activity;

    public ChildSubsectionProjectSimpleExpandableListAdapter(Context context, List<ContractGroup> list, MyGlobal global) {
    	this.inflater = LayoutInflater.from(context);
    	this.list = list;
    	this.global = global;
    	this.activity = (Activity) context;
    }
    
	public Object getChild(int groupPosition, int childPosition) {
		return list.get(groupPosition).getChildren().get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
            convertView = inflater.inflate(R.layout.childsubsectionproject_children, null);
        }
		ContractChild child = list.get(groupPosition).getChildren().get(childPosition);
		TextView unitProjectName = (TextView) convertView.findViewById(R.id.prj_unit_Project_name);
		unitProjectName.setText(child.getUnitRangeName());
		TextView subsectionProjectName = (TextView) convertView.findViewById(R.id.subsection_Project_name);
		subsectionProjectName.setText(child.getSubsectionProjectName());
		return convertView;
	}

	public int getChildrenCount(int groupPosition) {
		return list.get(groupPosition).getChildren().size();
	}

	public Object getGroup(int groupPosition) {
		return list.get(groupPosition);
	}

	public int getGroupCount() {
		return list.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (convertView == null) {
            convertView = inflater.inflate(R.layout.subsectionproject_group, null);
        }
		final ContractGroup group = list.get(groupPosition);
		TextView text = (TextView) convertView.findViewById(R.id.parent_group);
		text.setText(group.getTitle());
		convertView.findViewById(R.id.view_btn).setOnClickListener(new OnClickListener() {
			   
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.view_btn: 
					Intent intent=new Intent();
					intent.setClassName("lwh.activity","lwh.activity.SubitemProjectActivity");
					Bundle bundle=new Bundle();
					bundle.putString("prj_unit_range_id", group.getPrj_unit_range_id());
					bundle.putString("subsection", group.getSubsectionId());
					bundle.putString("childsub", group.getId());
			    	intent.putExtras(bundle);
			    	global.setChildSectionName(group.getTitle());
			    	//global.setPrj_id(group.getId());
			    	global.setChildsubsectionId(group.getId());
			    	activity.startActivityForResult(intent, 200);
					break;
				}
			}
			
		});
		return convertView;
	}

	public boolean hasStableIds() {
		return false;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

	
}
